package condominios.servlets.moroso;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import condominios.modelo.Persona;

/**
 * Servlet implementation class SugerenciaCambiarFilasServlet
 */
@WebServlet("/SugerenciaCambiarFilasServlet")
public class SugerenciaCambiarFilasServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
		int numInstancia = 0;
		int numFilas;
		int minRow = 0;
		int maxRow = 4;
		int numPag;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SugerenciaCambiarFilasServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		System.out.println("Dentro de doPost del servlet SugerenciaCambiarFilasSerlvet");
		HttpSession session = request.getSession();
		Persona vo = (Persona)session.getAttribute("user");
		
		int numPaginas;
				
		if (vo== null){
			RequestDispatcher rd = request.getRequestDispatcher("error.jsp");
			rd.forward(request, response);
			return;
		}
		//numFilas = (int) session.getAttribute("CANTIDAD_FILAS");
		System.out.println("Numero de Instancia:" + numInstancia);
		
			
		numPag=1;
			
		numFilas = Integer.parseInt(request.getParameter("selCanId"));
		System.out.println("NumeroFilas:" + numFilas);
		session.setAttribute("CANTIDAD_FILAS", numFilas);
		
		numInstancia += 1;		
				
		minRow = (numPag*numFilas)-numFilas;
		maxRow = minRow + (numFilas-1);
			
		System.out.println("Numero de FilaMin:" + minRow);
		System.out.println("Numero de FilasxPagina:" + numFilas);
		
		int numRegistros = (int) session.getAttribute("NUMEROREGISTROS");
		
		if (maxRow > (numRegistros-1)){
			
			maxRow = numRegistros-1; 
		}
		if (numRegistros % numFilas == 0){
			numPaginas = ((int) Math.floor(numRegistros/numFilas));
		}
		else {
			numPaginas = ((int) Math.floor(numRegistros/numFilas)) +1;
		}
		
		System.out.println("Numero de Registros:" + numRegistros);				
		// Guardar en el ambiente de request
		request.setAttribute("CUOTAS", session.getAttribute("LISTACUOTAS"));		
		request.setAttribute("MINROW", minRow);
		request.setAttribute("NUMEROPAGINAS",numPaginas);
		request.setAttribute("MAXROW", maxRow);
		request.setAttribute("NUMFILAS", numFilas);
		request.setAttribute("NUMPAG", 1);
		RequestDispatcher rd = request.getRequestDispatcher("sugerencia_buscar.jsp");
		rd.forward(request, response);	
	}

}
